﻿using System.Linq;

namespace Core.Features
{
    using System;
    using Base;
    using Contracts.Enums;
    using Contracts.Interfaces;
    using Resources;
    using System.Diagnostics;


    public sealed class MediumMissileLauncherDamage<T> : FeatureBase<object>
    {
        public MediumMissileLauncherDamage(){}

        public MediumMissileLauncherDamage(T value) 
            : base(value){ }

        /// <summary>
        /// Obtains the feature name
        /// </summary>
        public override string Name
        {
            get { return "Damage"; }
        }
        /// <summary>
        /// Obtains the feature group
        /// </summary>
        public override FeatureGroupsEnum FeatureGroup{ get { return FeatureGroupsEnum.Specific; } }
        /// <summary>
        /// Obtains the feature type
        /// </summary>
        public override FeaturesEnum FeatureType { get { return FeaturesEnum.MediumMissileLauncherDamage; } }
        /// <summary>
        /// Obtains or defines the feature unit
        /// </summary>
        public override string Unit { get { return Traduction.label_unit_percent; } }

        public override void SetModifiedValue(IAgent agent, IRobot robot)
        {
            if (agent == null || robot == null) return;

            // Bonuses from extensions without target analysis
            double extensionBonus = agent.Extensions
                .Where(e => e.GetType() != typeof(Extensions.TargetAnalysis))
                .SumModifiersValues(FeatureTypesEnum.Module, this.FeatureType);

            // Bonus from Target Analysis extension
            double targetAnalysisBonus = agent.Extensions
                .Where(e => e.GetType() == typeof(Extensions.TargetAnalysis) && e.Level > 0)
                .SumModifiersValues(FeatureTypesEnum.Module, this.FeatureType);

            // Bonus from robot
            double robotBonus = robot.Modifiers.SumModifiersValues(agent.Extensions, FeatureTypesEnum.Module, this.FeatureType);

            // Bonus from Tunning modules
            double tunnings = robot.Modules.Aggregate(1, (current, mod) => current * (1 + mod.Value), FeatureTypesEnum.Module, this.FeatureType, ModuleGroupsEnum.WeaponTunings);

            var final = (double)this.Value * (1 + (extensionBonus + robotBonus)) * (1 + targetAnalysisBonus) * tunnings;

            ModifiedValue = IsRounded
                ? Math.Round(final, 0, MidpointRounding.AwayFromZero)
                : Math.Round(final, 2);
        }
         
    }
}